package cn.ejiajunxy.manager.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.leo.utils.CheckUtils;

import cn.ejiajunxy.manager.app.CustomApplication;
import cn.ejiajunxy.manager.model.TokenModel;

/**
 * Created by zhangguobao on 2018/3/14.
 */

public class UserPrefManager {
    private static String TOKEN_PREFS = "userinfo";
    private static String USER_PREFS = "user";
    private static String AREA_PREFS = "area";
    private static final String ACCESS_TOKEN = "Access_Token";
    private static final String ACCESS_TOKEN_EXPIRES = "Access_Token_Expires";
    private static final String REFRESH_TOKEN = "Refresh_Token";
    private static final String AREA_ID = "area_id";
    private static final String AREA_NAME = "area_name";
    private static final String AREA_PARENT_ID = "parent_id";

    private static final String USER_FULLNAME = "FullName";
    private static final String USER_AVATAER = "avatar";
    private static final String USER_GENDER = "gender";

    private static final String USER_ACCOUNT = "account";

    public static void saveToken(Context context, TokenModel tokenModel) {
        SharedPreferences pref = getContext(context).getSharedPreferences(TOKEN_PREFS, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = pref.edit();
        edit.putString(ACCESS_TOKEN, CheckUtils.isEmpty(tokenModel) ? null : tokenModel.Access_Token);
        edit.putString(ACCESS_TOKEN_EXPIRES, CheckUtils.isEmpty(tokenModel) ? null : tokenModel.Access_Token_Expires);
        edit.putString(REFRESH_TOKEN, CheckUtils.isEmpty(tokenModel) ? null : tokenModel.Refresh_Token);
        edit.commit();
    }

    public static void saveToken(String tokenModel,Context context) {
        SharedPreferences pref = getContext(context).getSharedPreferences(TOKEN_PREFS, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = pref.edit();
        edit.putString(ACCESS_TOKEN, tokenModel);
        edit.commit();
    }


    public static void saveAccount(Context context, String account) {
        SharedPreferences pref = getContext(context).getSharedPreferences(USER_ACCOUNT, Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = pref.edit();
        edit.putString(USER_ACCOUNT, account);
        edit.commit();
    }

    public static String getAccount(Context context) {
        SharedPreferences pref = getContext(context).getSharedPreferences(USER_ACCOUNT, Context.MODE_PRIVATE);
        return pref.getString(USER_ACCOUNT, "");
    }


    public static String getToken(Context context) {
        SharedPreferences pref = getContext(context).getSharedPreferences(TOKEN_PREFS, Context.MODE_PRIVATE);
        return pref.getString(ACCESS_TOKEN, "");
    }



    private static Context getContext(Context context) {
        if (context == null) {
            context = CustomApplication.getAppContext();
        }
        return context;
    }
}
